package com.jboost.modules.file.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jboost.common.api.CommonAPI;
import com.jboost.common.api.vo.Result;
import com.jboost.common.aspect.annotation.AutoLog;
import com.jboost.common.file.constant.JboostFileConstant;
import com.jboost.common.system.base.controller.JBoostController;
import com.jboost.common.system.query.QueryGenerator;
import com.jboost.common.system.vo.DictModel;
import com.jboost.modules.file.entity.JboostFile;
import com.jboost.modules.file.service.IJboostFileService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description: jboost_file
 * @Author: cxn
 * @Date:   2021-05-11
 * @Version: V1.0
 */
@Api(tags="jboost_file",hidden=true)
@RestController
@RequestMapping("/file")
@Slf4j
public class JboostFileController extends JBoostController<JboostFile, IJboostFileService> {
	@Autowired
	private IJboostFileService fileService;
	@Autowired
	private CommonAPI commonAPI;
	
	/**
	 * 分页列表查询
	 *
	 * @param file
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "jboost_file-分页列表查询")
//	@ApiOperation(value="jboost_file-分页列表查询", notes="jboost_file-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(JboostFile file,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
		QueryWrapper<JboostFile> queryWrapper = QueryGenerator.initQueryWrapper(file, req.getParameterMap());
		Page<JboostFile> page = new Page<JboostFile>(pageNo, pageSize);
		IPage<JboostFile> pageList = fileService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param file
	 * @return
	 */
	@AutoLog(value = "jboost_file-添加")
//	@ApiOperation(value="jboost_file-添加", notes="jboost_file-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody JboostFile file) {
		fileService.save(file);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param file
	 * @return
	 */
	@AutoLog(value = "jboost_file-编辑")
//	@ApiOperation(value="jboost_file-编辑", notes="jboost_file-编辑")
	@PutMapping(value = "/edit/{id}")
	public Result<?> edit(@RequestBody JboostFile file) {
		fileService.updateById(file);
		return Result.OK("编辑成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "jboost_file-通过id查询")
//	@ApiOperation(value="jboost_file-通过id查询", notes="jboost_file-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@PathVariable(name="id",required=true) String id) {
		JboostFile file = fileService.getById(id);
		if(file==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(file);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param file
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, JboostFile file) {
        return super.exportXls(request, file, JboostFile.class, "jboost_file");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, JboostFile.class);
    }

    @GetMapping(value = "/qualificationType/list")
    public Result<?> qualificationTypeList(){
		List<DictModel> typeItems = commonAPI.queryDictItemsByCode(JboostFileConstant.QUALIFICATION_TYPE);
		JSONObject result = new JSONObject();
		result.put("records",typeItems);
		return Result.OK(result);
	}

}
